package net.neptech.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.neptech.exception.BusinessException;
import net.neptech.mapper.PositionMapper;
import net.neptech.mapper.StaffPositionMapper;
import net.neptech.model.sys.Position;
import net.neptech.model.sys.StaffPosition;
import net.neptech.page.PageQuery;
import net.neptech.page.TableDataInfo;
import net.neptech.service.PositionService;
import net.neptech.utils.BeanCopyUtils;
import net.neptech.vm.enums.ResultStatusEnum;
import net.neptech.vo.PositionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: liyuxiang
 * @Date: 2025/8/16 18:34
 * @Description: TODO
 */
@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    PositionMapper positionMapper;
    @Autowired
    StaffPositionMapper staffPositionMapper;

    @Override
    public List<Position> selectPositionAll() {
        return positionMapper.selectList(new LambdaQueryWrapper<>());
    }


    @Override
    public List<Long> selectPositionListByUserId(Long staffId) {
//        return positionMapper.selectList(new LambdaQueryWrapper<Position>()
//                                     .eq(Position::getStaffId, staffId)) // or whatever the correct field name is
//                             .stream()
//                             .map(Position::getPositionId)
//                             .collect(Collectors.toList());
        return null;
    }

    @Override
    public TableDataInfo<PositionVo> selectPositionListPage(Position position, PageQuery pageQuery) {

        Page<PositionVo> page = positionMapper.selectPositionListPage(position, pageQuery.build());
        return TableDataInfo.build(page);
    }

    @Override
    public void insertPosition(Position position) {
        positionMapper.insert(position);
    }

    @Override
    public void deletePositionById(Long positionId) {
        //查询是否已经被分配
        List<StaffPosition> staffPositions = staffPositionMapper.selectList(new LambdaQueryWrapper<StaffPosition>().eq(StaffPosition::getPositionId, positionId));
        if (staffPositions != null && !staffPositions.isEmpty()) {
            throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), "该岗位已经被分配,不能删除");
        }
        positionMapper.deleteById(positionId);
    }

    @Override
    @Transactional
    public void deletePositionByIds(Long... positionIds) {
        Arrays.stream(positionIds).forEach(positionId -> {
            List<StaffPosition> staffPositions = staffPositionMapper.selectList(new LambdaQueryWrapper<StaffPosition>().eq(StaffPosition::getPositionId, positionId));
            if (staffPositions != null && !staffPositions.isEmpty()) {
                throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), "该岗位已经被分配,不能删除");
            }
            positionMapper.deleteById(positionId);
        });
    }

    @Override
    public PositionVo getPositionById(Long positionId) {
        return BeanCopyUtils.copyBean(positionMapper.selectById(positionId), PositionVo.class);
    }

    @Override
    public void updatePosition(Position position) {
        positionMapper.updateById(position);
    }
}
